package com.yanxinim.cn.store;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xuan.xuanhttplibrary.okhttp.HttpUtils;
import com.yanxinim.cn.AppConfig;
import com.yanxinim.cn.R;
import com.yanxinim.cn.fragment.StoreGoodsFragment;
import com.yanxinim.cn.store.adapter.TypeAdapter;
import com.yanxinim.cn.store.bean.ProductType;
import com.yanxinim.cn.ui.MainActivity;
import com.yanxinim.cn.ui.base.BaseActivity;
import com.yanxinim.cn.util.JsonTool;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class StoreHomeActivity extends BaseActivity {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_store_mine)
    ImageView ivStoreMine;
    @BindView(R.id.iv_store_car)
    ImageView ivStoreCar;
    @BindView(R.id.view_pager_store)
    ViewPager viewPagerStore;
    List<ProductType> productTypeList = new ArrayList<>();
    @BindView(R.id.rv_type_data)
    RecyclerView rvTypeData;
    @BindView(R.id.iv_store_home)
    ImageView ivStoreHome;
    private List<Fragment> fragments = new ArrayList<>();
    private TypeAdapter typeAdapter;
    private StoreGoodsFragment nowFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.a_store_home_activity);
        ButterKnife.bind(this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(RecyclerView.HORIZONTAL);
        rvTypeData.setLayoutManager(linearLayoutManager);

//        productTypeList.add(new ProductType("-1", "123", "全部", "123", "-1", "1", true));
        getProduceType();


    }

    @OnClick({R.id.iv_back, R.id.iv_store_mine, R.id.iv_store_car,R.id.iv_store_home})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_store_mine:
                startActivity(new Intent(StoreHomeActivity.this, StoreMineActivity.class));
                break;
            case R.id.iv_store_car:
                startActivity(new Intent(StoreHomeActivity.this, StoreCartActivity.class));
                break;
            case R.id.iv_store_home:
                if (nowFragment != null){
                    viewPagerStore.setCurrentItem(0);
                }
                break;
        }
    }

    private void getProduceType() {
        HttpUtils.get().url(AppConfig.STORE_CATEGORY)
                .params("status", "1")
                .build().execute(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                JSONObject jsonObject = JSON.parseObject(result);
                if (jsonObject.getInteger("resultCode") == 1) {
                    List<ProductType> productTypes = JsonTool.jsonToList(jsonObject.getString("data"), ProductType.class);
                    runOnUiThread(() -> {
                        if (productTypes != null) {
                            productTypeList.addAll(productTypes);
                            if (productTypeList.size() > 0) {
                                ProductType productType = productTypeList.get(0);
                                productType.setSelected(true);
                                productTypeList.set(0, productType);
                            }
                        }

                        for (int i = 0; i < productTypeList.size(); i++) {
                            StoreGoodsFragment storeGoodsFragment = StoreGoodsFragment.getInstance(productTypeList.get(i).getId(),productTypeList.get(i).getCategoryname());
                            fragments.add(storeGoodsFragment);
                        }

                        if (fragments.size() == 0){
                            return;
                        }
                        nowFragment = (StoreGoodsFragment) fragments.get(0);
                        typeAdapter = new TypeAdapter(StoreHomeActivity.this, productTypeList);
                        rvTypeData.setAdapter(typeAdapter);
                        typeAdapter.setItemOnClickListener(new TypeAdapter.ItemOnClickListener() {
                            @Override
                            public void onItemClick(ProductType newType, int position) {
                                typeAdapter.updateItem(position);
                                viewPagerStore.setCurrentItem(position);
                                nowFragment = (StoreGoodsFragment) fragments.get(position);
                            }
                        });
                        ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(), fragments);
                        viewPagerStore.setAdapter(viewPagerAdapter);
                        viewPagerStore.setCurrentItem(0);
                        viewPagerStore.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                            @Override
                            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                            }

                            @Override
                            public void onPageSelected(int position) {
                                if (typeAdapter != null) {
                                    typeAdapter.updateItem(position);
                                }
                                nowFragment = (StoreGoodsFragment) fragments.get(position);
                                rvTypeData.scrollToPosition(position);
                            }

                            @Override
                            public void onPageScrollStateChanged(int state) {

                            }
                        });
                    });
                }
                Log.i("qin_store", result);
            }
        });
    }
}
